package design.simdi.code.final_paper.site.panels.proiecte.idei;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Restrictions;

import design.simdi.code.final_paper.core.daos.IdeaDao;
import design.simdi.code.final_paper.core.entities.Idea;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;

/**
 * 
 * @author Marius Dumitriu - (Jun 18, 2010 - 1:28:55 AM)
 * 
 */
public class IdeiTabPanel extends PagePanel {

	private static final long serialVersionUID = 1L;
	@SpringBean
	private IdeaDao ideaDao;

	public IdeiTabPanel(String id) {
		super(id);
		InjectorHolder.getInjector().inject(this);
		((WicketApplication) getApplication()).setShowErrors(true);
	}

	@Override
	public void panelInit() {
		List<IColumn<Idea>> columns = new ArrayList<IColumn<Idea>>();

		columns.add(new PropertyColumn<Idea>(new ResourceModel("name"), "name", "name"));
		columns.add(new PropertyColumn<Idea>(new ResourceModel("keywordsAsString"), "keywordsAsString"));
		columns.add(new AbstractColumn<Idea>(new ResourceModel("description")) {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<Idea>> cellItem, String componentId, IModel<Idea> rowModel) {
				int max = 45;
				boolean puncte = true;
				String text = "";
				Idea idee = rowModel.getObject();
				if (idee.getDescription() == null) {

					cellItem.add(new Label(componentId, "-"));

				} else {

					if (idee.getDescription().length() < max) {
						max = idee.getDescription().length();
						puncte = false;
					}
					text = idee.getDescription().substring(0, max);
					if (puncte)
						text += " ... ";

					cellItem.add(new Label(componentId, text));
				}
			}
		});

		SortableIdeaDataProvider sortableIdeaDataProvider = new SortableIdeaDataProvider();
		sortableIdeaDataProvider.getCriterii().add(Restrictions.eq("person.id", getCurrentPerson().getId()));

		add(new MyDataTable<Idea>("table", columns, sortableIdeaDataProvider, getRowsPerPage()) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public ActionToolbar getActionToolbar() {
				return new IdeaActionToolbar(this);
			}
		});
	}

	public void setIdeaDao(IdeaDao ideaDao) {
		this.ideaDao = ideaDao;
	}

	public IdeaDao getIdeaDao() {
		return ideaDao;
	}

	public int getRowsPerPage() {
		return 10;
	}
}